package longrise.phone.com.bjjt_henan.compensate;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerCar;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.StringUtils;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.Constant.URLConstant;
import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.CodeCountHelper;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;
import longrise.phone.com.bjjt_henan.Utils.UiUtils;
import longrise.phone.com.bjjt_henan.main.ServiceCenterView;
import longrise.phone.com.bjjt_henan.widget.PromptDialog;

/**
 * 定损报告书，Created by Administrator on 2016/5/12.
 */

public class CheckLossReportView extends LFView implements View.OnClickListener {
    private Context context = null;
    private View view = null;
    private Button bt_tv_checkloss3;
    private Button bt_checklossreport1;
    private TextView tv_checkloss1;
    private TextView tv_checkloss2;
    private TextView tv_checkloss3;
    private TextView tv_checkloss4;
    private TextView tv_checkloss5;
    private TextView tv_checkloss6;
    private TextView tv_checkloss7;
    private TextView tv_checkloss8;
    private TextView tv_checkloss9;
    private TextView tv_checkloss10;
    private Button bt_checklossreport2;
    private BaseApplication mApplication;
    private TextView tv_checklossreport1;
    private TextView tv_checklossreport2;
    private String appCaseno;
    private EntityBean[] checkBean;
    private String dutytype1;
    private String dutytype2;
    private LinearLayout hn_btn_back;
    private TextView tv_title;
    private Dialog processDialog;
    private EditText et_checkloss1;
    private EditText et_checkloss2;
    private Button bt_checklossreport3;
    private String ifSingle;
    private TextView tv_checklosshe;
    private LinearLayout ll_checklosshe;

    private CodeCountHelper otherHelper;

    private CodeCountHelper myHelper;

    private TextView infoTV;

    private EntityBean otherBean = null;//对方信息
    private EntityBean myBean = null;//我的信息

    public CheckLossReportView(Context c, EntityBean[] beans, String appCaseno) {
        super(c);
        this.context = c;
        this.mApplication = ((MainActivity) c).getAppData();
        this.appCaseno = appCaseno;
        this.checkBean = beans;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {

    }

    @Override
    public View getView() {
        return view;
    }

    public void setUnqualifylist(String ifSingle) {
        this.ifSingle = ifSingle;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
    }

    private void initView() {
        view = View.inflate(context, R.layout.view_checklossreport, null);
        bt_checklossreport1 = (Button) view.findViewById(R.id.bt_checklossreport1);
        bt_checklossreport2 = (Button) view.findViewById(R.id.bt_checklossreport2);
        bt_checklossreport3 = (Button) view.findViewById(R.id.bt_checklossreport3);
        tv_checklossreport1 = (TextView) view.findViewById(R.id.tv_checklossreport1);
        tv_checklossreport2 = (TextView) view.findViewById(R.id.tv_checklossreport2);
        et_checkloss1 = (EditText) view.findViewById(R.id.et_checkloss1);
        et_checkloss2 = (EditText) view.findViewById(R.id.et_checkloss2);
        tv_checkloss1 = (TextView) view.findViewById(R.id.tv_checkloss1);
        tv_checkloss2 = (TextView) view.findViewById(R.id.tv_checkloss2);
        tv_checkloss3 = (TextView) view.findViewById(R.id.tv_checkloss3);
        tv_checkloss4 = (TextView) view.findViewById(R.id.tv_checkloss4);
        tv_checkloss5 = (TextView) view.findViewById(R.id.tv_checkloss5);
        tv_checkloss6 = (TextView) view.findViewById(R.id.tv_checkloss6);
        tv_checkloss7 = (TextView) view.findViewById(R.id.tv_checkloss7);
        tv_checkloss8 = (TextView) view.findViewById(R.id.tv_checkloss8);
        tv_checkloss9 = (TextView) view.findViewById(R.id.tv_checkloss9);
        tv_checkloss10 = (TextView) view.findViewById(R.id.tv_checkloss10);
        infoTV = (TextView) view.findViewById(R.id.lossreport_tv_info);
        hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("定损报告书");
        //初始化
        if (TextUtils.equals("0", ifSingle)) {//单车
            myBean = checkBean[0];
            tv_checklosshe = (TextView) view.findViewById(R.id.tv_checklosshe);
            tv_checklosshe.setVisibility(View.GONE);
            TextView dividerTV = (TextView) view.findViewById(R.id.tv_checklos_divider);
            dividerTV.setVisibility(View.GONE);
            ll_checklosshe = (LinearLayout) view.findViewById(R.id.ll_checklosshe);
            ll_checklosshe.setVisibility(View.GONE);
            TextView selfTitlteTV = (TextView) view.findViewById(R.id.self_loss_title);
            selfTitlteTV.setVisibility(View.GONE);
            tv_checkloss7.setText("全责");//单车默认全责
        } else {
            for (int i = 0; i < checkBean.length; i++) {
                String ownother = checkBean[i].getString("party");
                if (TextUtils.equals(ownother, "1")) {
                    myBean = checkBean[i];
                } else if (TextUtils.equals(ownother, "0")) {
                    otherBean = checkBean[i];
                }
            }
            //对方数据
            String carownphone2 = (String) otherBean.get("carownphone");
            String carownname2 = (String) otherBean.get("carownname");
            String casecarno2 = (String) otherBean.get("casecarno");
            String feepaymoney2 = (String) otherBean.get("feepaymoney");
            String feepaymoney1 = (String) myBean.get("feepaymoney");
            dutytype2 = (String) otherBean.get("dutytype");

            String info = null;
            //根据保险公司返回的信息填充界面
            if ("0".equals(dutytype2)) {
                tv_checkloss2.setText("全责");
                tv_checkloss7.setText("无责");
                info = String.format(context.getResources().getString(R.string.fullduty_info), feepaymoney1);
                infoTV.setText(CommontUtils.changeStrColor(info, feepaymoney1, Color.RED));
            }
            if ("1".equals(dutytype2)) {
                tv_checkloss2.setText("无责");
                tv_checkloss7.setText("全责");
                info = String.format(context.getResources().getString(R.string.fullduty_info), feepaymoney2);
                infoTV.setText(CommontUtils.changeStrColor(info, feepaymoney2, Color.RED));
            }
            if ("2".equals(dutytype2)) {
                tv_checkloss2.setText("同责");
                tv_checkloss7.setText("同责");
                infoTV.setText(context.getResources().getString(R.string.equalduty_info));
            }
            if (TextUtils.isEmpty(feepaymoney2)) {
                feepaymoney2 = "-";
            }
            tv_checkloss1.setText(feepaymoney2);
            tv_checkloss3.setText(carownname2);
            tv_checkloss4.setText(casecarno2);
            tv_checkloss5.setText(carownphone2);
        }
        //本方数据
        String carownphone1 = (String) myBean.get("carownphone");
        dutytype1 = (String) myBean.get("dutytype");
        String carownname1 = (String) myBean.get("carownname");
        String casecarno1 = (String) myBean.get("casecarno");
        String feepaymoney1 = (String) myBean.get("feepaymoney");
        if (TextUtils.isEmpty(feepaymoney1)) {
            feepaymoney1 = "-";
        }
        tv_checkloss6.setText(feepaymoney1);
        tv_checkloss8.setText(carownname1);
        tv_checkloss9.setText(casecarno1);
        tv_checkloss10.setText(carownphone1);

        //单车的信息提示
        if (TextUtils.equals("0", ifSingle)) {
            LinearLayout infoLayout = (LinearLayout) view.findViewById(R.id.ll_checkloss);
            infoLayout.removeAllViews();

            TextView tv = new TextView(context);
            tv.setTextColor(Color.WHITE);
            String info = "定损金额为" + feepaymoney1 + "元，请出示收据！";
            tv.setText(CommontUtils.changeStrColor(info, feepaymoney1, Color.RED));

            infoLayout.addView(tv);
        }
    }

    private void regEvent(boolean b) {
        if (null != bt_checklossreport1) {
            bt_checklossreport1.setOnClickListener(b ? this : null);
        }
        if (null != bt_checklossreport2) {
            bt_checklossreport2.setOnClickListener(b ? this : null);
        }
        if (null != bt_checklossreport3) {
            bt_checklossreport3.setOnClickListener(b ? this : null);
        }
        if (null != tv_checklossreport1) {
            tv_checklossreport1.setOnClickListener(b ? this : null);
        }
        if (null != tv_checklossreport2) {
            tv_checklossreport2.setOnClickListener(b ? this : null);
        }
        if (null != hn_btn_back) {
            hn_btn_back.setOnClickListener(b ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_checklossreport1:
                if (TextUtils.equals("1", ifSingle)) {
                    if (TextUtils.isEmpty(et_checkloss1.getText().toString().trim())) {
                        UiUtil.showToast(context, "请输入乙方验证码");
                        return;
                    }
                }
                if (TextUtils.isEmpty(et_checkloss2.getText().toString().trim())) {
                    if (TextUtils.equals("0", ifSingle)) {
                        UiUtil.showToast(context, "请输入本方验证码");
                    } else {
                        UiUtil.showToast(context, "请输入甲方验证码");
                    }
                    return;
                }
                UiUtils.showDialog(context, null, null, "确定认可定损金额", "返回", "确认",
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                return;
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                checkBank(); //进入银行卡信息
                            }
                        });
                break;
            case R.id.bt_checklossreport2:
//                showReportDialog();
                ServiceCenterView view = new ServiceCenterView(context, appCaseno);
                FrameworkManager.getInstance().showNewForm(context, view);
                break;
            case R.id.bt_checklossreport3:
//                if (TextUtils.equals("1", ifSingle)) {
//                    if (TextUtils.isEmpty(et_checkloss1.getText().toString().trim())) {
//                        UiUtil.showToast(context, "请输入对方验证码");
//                        return;
//                    }
//                }
//                if (TextUtils.isEmpty(et_checkloss2.getText().toString().trim())) {
//                    UiUtil.showToast(context, "请输入本方验证码");
//                    return;
//                }
                UiUtils.showDialog(context, R.mipmap.warn, null, "确定放弃理赔？", "返回", "确认",
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                return;
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                quitClaims(); //金额较小。放弃理赔

                            }
                        });
                break;
            case R.id.tv_checklossreport1:
                //对方发送验证码
                getCode(tv_checkloss5.getText().toString().trim(), 1);
                break;
            case R.id.tv_checklossreport2:
                //本方发送验证码
                getCode(tv_checkloss10.getText().toString().trim(), 0);
                break;
            case R.id.hn_btn_back:
                OnDestroy();
                closeForm();
                break;
        }
    }

    //保险电话报案
    private void giveupIsass() {
        BaseApplication application = ((MainActivity) context).getAppData();
        String uid = application.getKckpName();
        String token = application.getToken();
        DialogUtils.showDialog(context);
        ApiImpI.getInstance(context).giveupIsass(appCaseno, uid, token, new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                DialogUtils.closeDialog();
                if (!TextUtils.isEmpty(error)) {
                    UiUtil.showToast(context, error);
                }
            }

            @Override
            public void onSuccess(Object result) {
                DialogUtils.closeDialog();
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                if (TextUtils.equals(restate, "1")) {
                    ((MainActivity) context).backToMainView();
                }
                String redes = bean.getString("redes");
                if (!TextUtils.isEmpty(redes)) {
                    UiUtil.showToast(context, redes);
                }
            }
        });
    }

    private PromptDialog dialog;

    private void showReportDialog() {
        if (null == dialog) {
            String title = context.getString(R.string.insurance_wait);
            String content = context.getString(R.string.report_content);
            dialog = new PromptDialog(context);
            dialog.setCanceledOnTouchOutside(false);
            dialog.setConfirm("确定", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    giveupIsass();
                }
            });
            dialog.setCancel("取消", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.setTitleNoIcon(title);
            dialog.setInfo(content);
        }
        dialog.show();
    }

    /**
     * 理赔金额较小，联网放弃理赔
     */
    private void quitClaims() {
        processDialog = UiUtil.showProcessDialog(context, "提交信息中...");
        EntityBean bean = new EntityBean();
        bean.set("uid", mApplication.getKckpName());
        bean.set("token", mApplication.getToken());
        bean.set("appcaseno", appCaseno);

        EntityBean bfBean = new EntityBean();// 本方数据
        bfBean.set("feepaymoney", tv_checkloss6.getText().toString().trim());
        bfBean.set("dutytype", tv_checkloss7.getText().toString().trim());
        bfBean.set("casecarno", tv_checkloss9.getText().toString().trim());
        bfBean.set("carownphone", tv_checkloss10.getText().toString().trim());
        bfBean.set("code", et_checkloss2.getText().toString().trim());

        EntityBean[] carbeans = null;//车主信息数组

        if (TextUtils.equals("1", ifSingle)) {
            EntityBean dfBean = new EntityBean();// 对方数据
            dfBean.set("feepaymoney", tv_checkloss1.getText().toString().trim()); // 定损金额
            dfBean.set("dutytype", tv_checkloss2.getText().toString().trim());  // 责任类型
            dfBean.set("casecarno", tv_checkloss4.getText().toString().trim());  // 案件车牌号
            dfBean.set("carownphone", tv_checkloss5.getText().toString().trim()); //手机号
            dfBean.set("code", et_checkloss1.getText().toString().trim()); // 验证码

            carbeans = new EntityBean[]{bfBean, dfBean};
        } else {
            carbeans = new EntityBean[]{bfBean};
        }
        bean.set("carbeans", carbeans);
        LoadDataManagerCar.getInstance(context).callService(null,
                mApplication.getServerUrl(), URLConstant.KCKPDISAGREE, bean, new LoadDataManagerFather.OnRequestCompleteListener() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                        EntityBean bean = (EntityBean) result;
                        String restate = bean.getString("restate");
                        String redes = bean.getString("redes");
                        UiUtil.showToast(context, redes);
                        if ("1".equals(restate)) {
                            ((MainActivity) context).backToMainView();
                        }
                    }

                    @Override
                    public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                        if (resultType == LoadDataManagerFather.ResultType.Failure) {
                            UiUtil.showToast(context, "连接超时，请检查网络后重试。");
                        } else if (resultType == LoadDataManagerFather.ResultType.NoNetWorks) {
                            UiUtil.showToast(context, "无网络连接，请检查您的网络");
                        }
                        return;
                    }

                    @Override
                    public void onFinished(String key, String service) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                    }
                });


    }

    //查询银行卡，进入银行卡信息
    private void checkBank() {
        processDialog = UiUtil.showProcessDialog(context, "提交信息中...");
        EntityBean bean = new EntityBean();
        bean.set("uid", mApplication.getKckpName());
        bean.set("token", mApplication.getToken());
        bean.set("appcaseno", appCaseno);

        EntityBean[] carbeans = null;//车主信息数组

        EntityBean bfBean = new EntityBean();// 本方数据
        bfBean.set("feepaymoney", tv_checkloss6.getText().toString().trim());
        bfBean.set("dutytype", tv_checkloss7.getText().toString().trim());
        bfBean.set("casecarno", tv_checkloss9.getText().toString().trim());
        bfBean.set("carownphone", tv_checkloss10.getText().toString().trim());
        bfBean.set("code", et_checkloss2.getText().toString().trim());

        if (TextUtils.equals("1", ifSingle)) {
            EntityBean dfBean = new EntityBean();// 对方数据
            dfBean.set("feepaymoney", tv_checkloss1.getText().toString().trim()); // 定损金额
            dfBean.set("dutytype", tv_checkloss2.getText().toString().trim());  // 责任类型
            dfBean.set("casecarno", tv_checkloss4.getText().toString().trim());  // 案件车牌号
            dfBean.set("carownphone", tv_checkloss5.getText().toString().trim()); //手机号
            dfBean.set("code", et_checkloss1.getText().toString().trim()); // 验证码

            carbeans = new EntityBean[]{bfBean, dfBean};
        } else {
            carbeans = new EntityBean[]{bfBean};
        }
        bean.set("carbeans", carbeans);
        LoadDataManagerCar.getInstance(context).callService(null,
                mApplication.getServerUrl(), URLConstant.KCKPAGREEDS, bean, new LoadDataManagerFather.OnRequestCompleteListener() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        Log.i("kckpAgreeds", "--->" + result.toString());
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                        EntityBean bean = (EntityBean) result;
                        String restate = bean.getString("restate");
                        String redes = bean.getString("redes");
                        if ("1".equals(restate)) {
                            //获取被保险人的姓名
                            EntityBean dataBan = bean.getBean("data");
                            if (dataBan != null) {
                                String ownName = dataBan.getString("insuredone");
                                String otherName = dataBan.getString("insuredtwo");
                                if (!TextUtils.isEmpty(ownName) && myBean != null) {
                                    myBean.set("carownname", ownName);
                                }
                                if (!TextUtils.isEmpty(otherName) && otherBean != null) {
                                    otherBean.set("carownname", otherName);
                                }
                            }

                            if (TextUtils.equals("0", ifSingle)) {//单车流程
                                BankCardInfoView bankcardinfoview = new BankCardInfoView(context, checkBean, appCaseno, "0");
                                FrameworkManager.getInstance().showNewForm(context, bankcardinfoview);
                            } else {
                                BankCardInfoView bankcardinfoview = new BankCardInfoView(context, checkBean, appCaseno);
                                FrameworkManager.getInstance().showNewForm(context, bankcardinfoview);
                            }
                        } else {
                            UiUtil.showToast(context, redes);
                        }
                    }

                    @Override
                    public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                        if (resultType == LoadDataManagerFather.ResultType.Failure) {
                            UiUtil.showToast(context, "连接超时，请检查网络后重试。");
                        } else if (resultType == LoadDataManagerFather.ResultType.NoNetWorks) {
                            UiUtil.showToast(context, "无网络连接，请检查您的网络");
                        }
                        return;
                    }

                    @Override
                    public void onFinished(String key, String service) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                    }
                });
    }

    /**
     * 获取验证码
     */
    private void getCode(String phonenum, final int flag) {
        if (!StringUtils.isPhone(phonenum)) {
            Toast.makeText(context, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        DialogUtils.showDialog(context);
        ApiImpI.getInstance(context.getApplicationContext()).getAgreementCode(phonenum, "2", new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                DialogUtils.closeDialog();
            }

            @Override
            public void onSuccess(Object result) {
                DialogUtils.closeDialog();
                EntityBean bean = (EntityBean) result;
                String redes = bean.getString("redes");
                if (!TextUtils.isEmpty(redes)) {
                    UiUtil.showToast(getContext(), redes);
                }
                String restate = bean.getString("restate");
                if (TextUtils.equals(restate, "1")) {
                    if (null == view) {
                        return;
                    }
                    if (flag == 0) {//己方
                        if (null == myHelper) {
                            myHelper = new CodeCountHelper(tv_checklossreport2);
                        }
                        myHelper.startCount();
                    } else if (flag == 1) {//对方
                        if (null == otherHelper) {
                            otherHelper = new CodeCountHelper(tv_checklossreport1);
                        }
                        otherHelper.startCount();
                    }
                }
            }
        });
    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        if (null != myHelper) {
            myHelper.destroy();
            myHelper = null;
        }
        if (null != otherHelper) {
            otherHelper.destroy();
            otherHelper = null;
        }
        processDialog = null;
        view = null;
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }
}
